package com.magic.gre.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.http.SslError;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.webkit.SslErrorHandler;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.magic.gre.R;
import com.magic.gre.base.activity.BaseMVPActivity;
import com.magic.gre.helper.Contract;
import com.magic.gre.js.wordscount.WordsJSCallBack;
import com.magic.gre.js.wordscount.WordsJsBridge;
import com.magic.gre.mvp.contract.WordCountContract;
import com.magic.gre.mvp.presenter.WordCountPresenterImpl;
import com.magic.gre.ui.dialog.ShareDialog;
import com.noname.lib_base_java.util.StatusBarUtil;

import butterknife.BindView;

/**
 * Author: Lmoumou
 * Date: 2019-01-25 09:59
 * 文件名: WordCountActvity
 * 描述:
 */
public class WordCountActvity extends BaseMVPActivity<WordCountPresenterImpl> implements WordCountContract.View,
        WordsJSCallBack {

    @BindView(R.id.status_bar_v)
    View statusView;
    @BindView(R.id.mWebView)
    WebView mWebView;
//    @BindView(R.id.head_iv)
//    ImageView headIv;
//    @BindView(R.id.nick_tv)
//    TextView nickTv;

    public static void startThis(Context context) {
        Intent intent = new Intent(context.getApplicationContext(), WordCountActvity.class);
        context.startActivity(intent);
    }

    private String url = Contract.WORDS_COUNT_URL;
    //    private String url = "http://192.168.1.57:9999/zhenjing_GRE/share_learnCount.html";
    private WordsJsBridge jsBridge;
    private ShareDialog shareDialog;
    private String shareId;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_word_count;
    }

    @Override
    protected WordCountPresenterImpl creatPresenter() {
        return new WordCountPresenterImpl(this);
    }

    @Override
    protected boolean isImmerse() {
        return true;
    }

    @Override
    protected void netOverride() {

    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        StatusBarUtil.setHeight(this, statusView);


//        UserBean userInfo = Apphelper.getUserInfo();
//        if (userInfo!=null){
//            LoadImageUtils.loadImage(PathUtil.imgPath(userInfo.getPhoto()),headIv);
//            nickTv.setText(userInfo.getNickname());
//        }
        jsBridge = new WordsJsBridge(this);
        initWebView();
    }

    private void initWebView() {
        WebSettings webSettings = mWebView.getSettings();

        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        webSettings.setUseWideViewPort(true);
        webSettings.setLoadWithOverviewMode(true);

        webSettings.setJavaScriptEnabled(true);
        webSettings.setDomStorageEnabled(true);
        String appCachePath = getApplicationContext().getCacheDir().getAbsolutePath();
        webSettings.setAppCachePath(appCachePath);
        webSettings.setAllowFileAccess(true);
        webSettings.setAppCacheEnabled(true);

        webSettings.setBlockNetworkImage(false);//解决图片不显示
        webSettings.setSupportZoom(false);
        webSettings.setBuiltInZoomControls(false);
        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }

//        webView.loadDataWithBaseURL(null, content, "text/html", "utf-8", null);
        mWebView.loadUrl(url);

        mWebView.setWebViewClient(new WebViewClient() {

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);

            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                Log.e("LLSSQQ", "onPageFinished+url->" + url);
                mWebView.loadUrl("javascript:(function(){" +
                        "var objs = document.getElementsByTagName('img'); " +
                        "for(var i=0;i<objs.length;i++)  " +
                        "{"
                        + "var img = objs[i];   " +
                        "    img.style.maxWidth = '100%'; img.style.height = 'auto';  " +
                        "}" +
                        "})()");
            }

            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                super.onReceivedError(view, errorCode, description, failingUrl);

            }

            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
//                handler.proceed();
            }
        });
        mWebView.setOnLongClickListener(new View.OnLongClickListener() {

            @Override
            public boolean onLongClick(View v) {
                return true;
            }
        });


        mWebView.addJavascriptInterface(jsBridge, "AndroidNative");
    }

    /**
     * H5分享方法
     */
    @Override
    public void share() {
        if (shareDialog == null) {
            shareDialog = new ShareDialog();
        }

        shareDialog.showThis(getSupportFragmentManager(), ShareDialog.class.getSimpleName(),
                "背词统计", "背词统计数量", url + "?queryId=" + shareId,
                Contract.Share_IMG_URL);
    }

    @Override
    public void noLogin() {
        showLoglinDialog();
    }

    /**
     * 获取分享时需要的id
     *
     * @param shareId
     */
    @Override
    public void getAppUrlId(String shareId) {
        this.shareId = shareId;
    }
}
